import Header from "../pages/Header";
import CookieUtils from "pages/ruijing/common/utils/CookieUtils";
import urlHook from "pages/ruijing/common/utils/urlHook";
import UserApi from "@module/userApi";
import HttpUtils from "pages/ruijing/common/utils/HttpUtils";

function hmsHeader(props) {
	const commonHeaderConfig = useStoreState("commonHeaderConfig");
    const orgId = CookieUtils.getCookie("orgId");
    const orgnization = decodeURIComponent(CookieUtils.getCookie("organization"));
    const username = decodeURIComponent(CookieUtils.getCookie("username"));
    let permissions = JSON.parse(localStorage.getItem("STORE_USERPERMISSION"));

    function refreshCookieByFieldName(data, fieldName) {
        fieldName = fieldName || "user";
        Uts.setCookieMt({
            id: data[fieldName].id,
            username: encodeURIComponent(data[fieldName].realName),
            organization: encodeURIComponent(data[fieldName].organization.name),
            orgId: data[fieldName].organization.id,
            vm: data[fieldName].mobile,
            userEmail: data[fieldName].email,
            orgCode: data[fieldName].organization.code
        });
        if (data[fieldName].departments.length > 0) {
            Uts.setCookie("deptId", data[fieldName].departments[0].id);
        } else {
            Uts.setCookie("deptId", "");
        }
    }

    function getIdentity() {
        HttpUtils.getIdentity()
            .then(res => {
                console.log(res);
                if (res.user) {
                    Uts.storage.set(
                        "STORE_USERPERMISSION",
                        Uts.fetchObjectArrayToStringArray(
                            res.user.permissions,
                            "name"
                        )
                    );
                    UserApi.updateUserState(res.user);
                    refreshCookieByFieldName(res)
                    setTimeout(() => {
                        window.location.reload();
                    }, 100);
                }
                if (res.code == "1020") {
                    ZenUI.ST("请先登录", 2);
                    CookieUtils.clearAllCookiesAndStorage();
                    /*setTimeout(() => {
                        window.location.href = urlHook.hmsLogin;
                    }, 1000);*/
                }
            })
            .catch(err => {
                console.log(err);
            });
    }

    React.useEffect(() => {
        if(!(CookieUtils.getCookie("orgId")&&CookieUtils.getCookie("orgId")!=undefined&&CookieUtils.getCookie("orgId")!=null&&permissions&&permissions.length>0&&permissions.indexOf("HMS用户")>-1)) {
            getIdentity();
        }
    },[]);
    if(CookieUtils.getCookie("orgId")&&CookieUtils.getCookie("orgId")!=undefined&&CookieUtils.getCookie("orgId")!=null&&permissions&&permissions.length>0&&permissions.indexOf("HMS用户")>-1) {
        return (
            <Header
                orgid={orgId}
                orgnization={orgnization}
                username={username}
            ></Header>
        );
    }else{
    	return "";
	}
}


export default hmsHeader;
